import request from "@/utils/request";
import { ContractItem } from "./contract.api";
import { IObject } from "../common/common";
import httpRequest from "@/utils/requestUtil";

const BASE_URL = "/business/ledger";

const ContractLedgerAPI = {
  /** 获取台账记录分页数据 */
  getPage(queryParams?: PageQuery) {
    return httpRequest.get<PageResult<ContractLedgerRecord[]>>(
      `${BASE_URL}/searchRecord`,
      queryParams
    );
  },
  /**
   * 根据合同id查询所有台账
   */
  load(id: string) {
    return request<any, ContractLedgerDetail>({
      url: `${BASE_URL}/loadByContractId?contractId=${id}`,
      method: "get",
    });
  },
  /**
   * 修改台账
   * @param data 表单数据
   */
  reviewContract(data: ContractLedgerForm) {
    return request<any, boolean>({
      url: `${BASE_URL}/reviewContract`,
      method: "post",
      data,
    });
  },

  /**
   * 审批台账
   */
  approval(data: ContractLedgerApproveForm) {
    return request<any, boolean>({
      url: `${BASE_URL}/review`,
      method: "post",
      data,
    });
  },

  loadRenGong(id: string) {
    return request<any, ContractLedgerRecordItem[]>({
      url: `${BASE_URL}/loadRenGongItems?contractId=${id}`,
      method: "get",
    });
  },
  /**
   * 根据记录ID查询台账对比
   */
  loadCompareByRecordId(recordId: string) {
    return request<any, ContractLedgerRecordAndLast>({
      url: `${BASE_URL}/loadCompareByRecordId?recordId=${recordId}`,
      method: "get",
    });
  },
  /**
   * 根据合同ID查询最后台账设计，可能是原始合同的，也可能是合同变更的
   * @param contractId  合同id
   * @param purchase
   * @param reimburse
   * @param renGong
   * @returns
   */
  loadLastByContractId(
    contractId: string,
    purchase: boolean,
    reimburse: boolean,
    renGong: boolean
  ) {
    return request<any, ContractLedgerRecordItem[]>({
      url: `${BASE_URL}/loadLastByContractId?contractId=${contractId}&purchase=${purchase}&reimburse=${reimburse}&renGong=${renGong}`,
      method: "get",
    });
  },
};

export default ContractLedgerAPI;

/** 实例分页对象 */
export interface ContractLedgerDetail {
  /** ID */
  id: string;
  /** 合同ID */
  contractId: string;
  contractName: string;
  /** 调整金额 */
  adjustAmount: number;
  /** 是否有调整权限 */
  applyRole: boolean;
  /** 调整记录 */
  records: ContractLedgerRecord[];
  /** 设计最初的项 */
  originalItems: ContractItem[];
  /** 最新修改的记录项 */
  newItems: ContractLedgerRecordItem[];
}

export interface ContractLedgerForm {
  contractId: string;
  title: string;
  items: ContractLedgerRecordItem[];
}

export interface ContractLedgerRecord {
  id: string;
  contractLedgerId: string;
  contractId: string;
  contractName: string;
  title: string;
  userId: string;
  username: string;
  reviewUserId: string;
  reviewUsername: string;
  createTime: string;
  adjustAmount: number;
  status: number; //审核状态（0未审核，1通过，2未通过）
  remark: string; //审核备注
  items: ContractLedgerRecordItem[];
}

export interface ContractLedgerRecordItem {
  id?: string;
  contractLedgerId: string;
  recordId: string;
  contractId: string;
  resourceId: string;
  name: string;
  remark: string;
  unit: string;
  price: string | number;
  quantity: number;
  spec: string;
  code: string;
  itemType: string;
  purchaseTotalQuantity?: number;
  purchaseTotalAmount?: number;
  reimburseTotalQuantity?: number;
  reimburseTotalAmount?: number;
  [key: string]: any;
}

// 台账记录材料项(前端用)
export interface ContractLedgerRecordCompare {
  id?: string;
  name?: string;
  remark?: string;
  unit?: string;
  price?: string | number;
  quantity?: number;
  spec?: string;
  code?: string;
  itemType?: string;
  /** 以下为对比记录 */
  operation?: number; // 操作类型（1增加，2修改，3删除）
  originalItem?: ContractLedgerRecordCompare; // 原始记录项（仅存变动过的）
  [key: string]: any;
}

/** 台账修改审批表单 */
export interface ContractLedgerApproveForm {
  recordId: string;
  status: number; //1：通过，2：驳回
  remark: string;
}

/** 台账记录和最新的记录对比 */
export interface ContractLedgerRecordAndLast extends ContractLedgerRecord {
  contractName: string;
  currentItems: ContractLedgerRecordItem[];
  lastItems: ContractLedgerRecordItem[];
}

export const ContractLedger_Status_Label: IObject = {
  0: "未审核",
  1: "已通过",
  2: "已驳回",
};
